﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.UI;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using DSkin.Forms;

namespace TY_Hoist.WinApp
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var capture = new VideoCapture(@"F:\新建文件夹 (8)\CamVideo.avi");
            int frameCount = Convert.ToInt32(capture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameCount));
            Image<Bgr, byte> lastFrame = null;
            Image<Bgr, byte> diffFrame = null;
            Image<Bgr, byte> bj = null;
            var isFirstFrame = false;
            int i = 0;
            while (i < frameCount)
            {
                using (Image<Bgr, byte> nextFrame = capture.QueryFrame().ToImage<Bgr, byte>())
                {
                    i++;
                    //var resultFrame = new Mat(nextFrame.Rows, nextFrame.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 1);
                    //CvInvoke.CvtColor(nextFrame, resultFrame, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
                    //var resultFrame1 = new Mat(nextFrame.Rows, nextFrame.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 1);
                    //CvInvoke.GaussianBlur(resultFrame, resultFrame1, new Size(21, 21), 0);
                    //pictureBox1.Image = nextFrame.ToBitmap();
                    var resultFrame2 = new Mat(nextFrame.Rows, nextFrame.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 1);
                    if (isFirstFrame)
                    {
                        //pictureBox2.Image = lastFrame.ToBitmap();
                        CvInvoke.AccumulateWeighted(nextFrame, resultFrame2, 0.3,null);
                        diffFrame = nextFrame.AbsDiff(lastFrame);
                        var moves = diffFrame.CountNonzero()[0];

                        //pictureBox3.Image = diffFrame.ToBitmap();
                        this.Invoke(new Action(() =>
                        {
                            pictureBox1.Image = nextFrame.ToBitmap();
                            label1.Text = moves.ToString();
                            richTextBox1.Text = richTextBox1.Text + System.Environment.NewLine + moves.ToString();
                        }));

                        //setPictureBoxControlImage(pb, nextFrame.ToBitmap());
                        //if (moves > 20)
                        //    label1.Text = "moved!";
                        //else
                        //    label1.Text = "sleeps...";

                    }
                    else
                    {
                        bj = nextFrame.Clone();
                    }

                    lastFrame = nextFrame.Clone();
                    isFirstFrame = true;
                }
            }


        }
    }
}
